

## tabellen varnames? 
# zip, dataverse


library(MASS)
library(stargazer)
library(ggplot2)
library(sjPlot)
library(sjmisc)
library(ggeffects)
library(ggpubr)

dir.create("plots")
dir.create("tables")

load("insult.rdata")

load("critic.rdata")

data1$party_pol<-ifelse(data1$party_pol=="DIE GR\xdcNEN","DIE GRÜNEN",data1$party_pol)
data2$party_pol<-ifelse(data2$party_pol=="DIE GR\xdcNEN","DIE GRÜNEN",data1$party_pol)

data1$party_pol <- as.factor(data1$party_pol)

data1$party_pol <- as.factor(data1$party_pol)
data1$party_pol <- relevel(data1$party_pol, ref = "FDP")

data2$party_pol <- as.factor(data2$party_pol)
data2$party_pol <- relevel(data2$party_pol, ref = "FDP")

data1$gender_pol <-as.factor(data1$gender_pol)
data1$gender_pol <-relevel(data1$gender_pol,ref="male")
data2$gender_pol <-as.factor(data2$gender_pol)
data2$gender_pol <-relevel(data2$gender_pol,ref="male")

data1$sent_criticism<-data1$sent
data2$sent_insult<-data2$sent
data1$received_criticism<-data1$received
data2$received_insult<-data2$received
#BASELINE MODELS
c_critic_int <- glm.nb(received_criticism ~ sent_criticism * gender_pol + party_pol + user_total + total_pol + vip + visible_minority, 
                       data = data1, 
                       control = glm.control(maxit = 500), 
                       init.theta = 1.0)

c_insult_int <- glm.nb(received_insult ~ sent_insult * gender_pol + party_pol + user_total + total_pol + vip + visible_minority, 
                       data = data2, 
                       control = glm.control(maxit = 500), 
                       init.theta = 1.0)


#WITHOUT INTERACTION
c_critic <- glm.nb(received_criticism ~ sent_criticism + gender_pol + party_pol + user_total + total_pol + vip + visible_minority,data=data1,control = glm.control(maxit = 500), init.theta = 1.0)
c_insult <- glm.nb(received_insult ~ sent_insult + gender_pol + party_pol + user_total + total_pol + vip + visible_minority, data=data2,control = glm.control(maxit = 500), init.theta = 1.0)

# WITH INTERACTION
c_critic_int <- glm.nb(received_criticism ~ sent_criticism * gender_pol + party_pol + user_total + total_pol + vip + visible_minority,data=data1,control = glm.control(maxit = 500), init.theta = 1.0)
c_insult_int <- glm.nb(received_insult ~ sent_insult * gender_pol + party_pol + user_total + total_pol + vip + visible_minority,data=data2, control = glm.control(maxit = 500), init.theta = 1.0)

yvarnames<-c("Criticism to Pol","Insult to Pol")

xvarnames<-c("Criticism from Pol",
"Insult from Pol" ,
"Woman Pol" ,
"Alternative for Germany" ,
"Christian Democratic Union",
"Christian Social Union",
"The Greens"  ,
"The Left" ,
"Social Democratic Party" ,
"Total Mentions",
"Total Tweets Pol" ,
"VIP" ,
"Identifiable Minority")

xvarnames_int<-c("Criticism from Pol",
             "Insult from Pol" ,
             "Woman Pol" ,
             "Alternative for Germany" ,
             "Christian Democratic Union",
             "Christian Social Union",
             "The Greens"  ,
             "The Left" ,
             "Social Democratic Party" ,
             "Total Mentions",
             "Total Tweets Pol" ,
             "VIP" ,
             "Identifiable Minority",
             "Woman Pol: Criticism",
             "Woman Pol: Insult")


stargazer(c_critic_int,c_insult_int,out = "tables/reg2_vip",type= "latex")
stargazer(c_critic,c_insult,out = "tables/reg1_vip.tex",type= "latex",dep.var.labels =   yvarnames,covariate.labels = xvarnames,label = "reg1")
stargazer(c_critic_int,c_insult_int,out = "tables/reg2_vip.tex",type = "latex",dep.var.labels =  yvarnames,covariate.labels = xvarnames_int,label="reg2")


# Interaction Plot vip --------------------------------------------------------

# WITH INTERACTION
c_critic_int <- glm.nb(received ~ sent * gender_pol + party_pol + user_total + total_pol + vip + visible_minority, 
                       data = data1, 
                       control = glm.control(maxit = 500), 
                       init.theta = 1.0)

c_insult_int <- glm.nb(received ~ sent * gender_pol + party_pol + user_total + total_pol + vip + visible_minority, 
                       data = data2, 
                       control = glm.control(maxit = 500), 
                       init.theta = 1.0)

# Effekte, wie gehabt
plot_critic_ef <- plot_model(c_critic_int, type = "int", terms = c("sent", "gender_pol"),se=T )
plot_critic_ef<-plot_critic_ef + scale_color_manual(values = c("female" = "#F8766D", "male" = "#00BFC4"))
plot_critic_ef

plot_insult_ef <- plot_model(c_insult_int, type = "int", terms = c("sent", "gender_pol"),se=T)
plot_insult_ef<-plot_insult_ef + scale_color_manual(values = c("female" = "#F8766D", "male" = "#00BFC4"))
plot_critic_ef


g1<-plot(plot_insult_ef, facet = F)+
  ggtitle("")+
  ylab("Count of Received Insults")+
  scale_y_continuous()+
  xlab("Number of own Insults")+
  theme(axis.text.x = element_text(family = "serif",
                                   vjust = 0.5,
                                   angle = 0,
                                   size=12),
        legend.position = c(0.75, 0.8),
        plot.title = element_text(hjust = 0.5, family = "serif"),
        axis.text.y = element_text(family = "serif"),
        axis.title.x = element_text(family = "serif"),
        axis.title.y = element_text(family = "serif"),
        strip.text.x = element_text(family = "serif"),
        legend.text = element_text(family = "serif",
                                   size=12),
        legend.title = element_text(family = "serif", 
                                    size=12),
        panel.spacing = unit(1, "lines"),
  )+labs(color='Gender')
g1

g2<-plot(plot_insult_ef, facet = F)+
  ggtitle("")+
  ylab("Count of Received Insults (log10 scale)")+
  scale_y_continuous(trans='log10')+
  xlab("Number of own Insults")+
  theme(axis.text.x = element_text(family = "serif",
                                   vjust = 0.5,
                                   angle = 0,
                                   size=12),
        legend.position = c(0.75, 0.8),
        plot.title = element_text(hjust = 0.5, family = "serif"),
        axis.text.y = element_text(family = "serif"),
        axis.title.x = element_text(family = "serif"),
        axis.title.y = element_text(family = "serif"),
        strip.text.x = element_text(family = "serif"),
        legend.text = element_text(family = "serif",
                                   size=12),
        legend.title = element_text(family = "serif", 
                                    size=12),
        panel.spacing = unit(1, "lines"),
  )+
  labs(color='Gender')
g2

g3<-plot(plot_critic_ef, facet = F)+
  ggtitle("")+
  ylab("Count of Received Criticism")+
  scale_y_continuous()+
  xlab("Number of own Criticism")+
  theme(axis.text.x = element_text(family = "serif",
                                   vjust = 0.5,
                                   angle = 0,
                                   size=12),
        legend.position = c(0.75, 0.8),
        plot.title = element_text(hjust = 0.5, family = "serif"),
        axis.text.y = element_text(family = "serif"),
        axis.title.x = element_text(family = "serif"),
        axis.title.y = element_text(family = "serif"),
        strip.text.x = element_text(family = "serif"),
        legend.text = element_text(family = "serif",
                                   size=12),
        legend.title = element_text(family = "serif", 
                                    size=12),
        panel.spacing = unit(1, "lines"),
  )+
  labs(color='Gender')

g4<-plot(plot_critic_ef, facet = F)+
  ggtitle("")+
  ylab("Count of Received Criticism (log10 scale)")+
  scale_y_continuous(trans='log10')+
  xlab("Number of own Criticism")+
  theme(axis.text.x = element_text(family = "serif",
                                   vjust = 0.5,
                                   angle = 0,
                                   size=12),
        legend.position = c(0.75, 0.8),
        plot.title = element_text(hjust = 0.5, family = "serif"),
        axis.text.y = element_text(family = "serif"),
        axis.title.x = element_text(family = "serif"),
        axis.title.y = element_text(family = "serif"),
        strip.text.x = element_text(family = "serif"),
        legend.text = element_text(family = "serif",
                                   size=12),
        legend.title = element_text(family = "serif", 
                                    size=12),
        panel.spacing = unit(1, "lines"),
  )+
  labs(color='Gender')

gf1<-ggarrange(g3,g4,ncol=2,nrow=1,common.legend = TRUE,legend = "bottom")
gf2<-ggarrange(g1,g2,ncol=2,nrow=1,common.legend = TRUE,legend = "bottom")


ggsave(gf1,filename="plots/interactions_crit.png",dpi = 300,width = 10,height = 8,)
ggsave(gf2,filename="plots/interactions_ins.png",dpi = 300,width = 10,height = 8)

